/*
 * Created by JFormDesigner on Fri Nov 22 14:57:06 CST 2024
 */

package com.hafu.view.kehu;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

import com.hafu.model.KhInfo;
import com.hafu.service.KhInfoService;
import com.hafu.util.StringUtil;
import net.miginfocom.swing.*;

import java.awt.*;
import java.util.ArrayList;

/**
 * @author aa
 */
public class ManageKeHuFrm extends JFrame {
    private KhInfoService khInfoService=new KhInfoService();
    private int chooseId;
    public ManageKeHuFrm() {
        initComponents();
        fillTable();
    }

    private void fillTable() {
        // TODO add your code here
        DefaultTableModel dtm=(DefaultTableModel)table1.getModel();
        dtm.setRowCount(0);
        ArrayList<KhInfo> khInfoList=khInfoService.getKhInfoByName(new KhInfo());
        for (KhInfo khInfo : khInfoList){
            Object[] rowData={khInfo.getId(),khInfo.getName(),khInfo.getTel()};
            dtm.addRow(rowData);
        }

    }

    private void button1(ActionEvent e) {
        // TODO add your code here 查询事件
        DefaultTableModel dtm=(DefaultTableModel)table1.getModel();
        dtm.setRowCount(0);
        String name=textField1.getText();
        KhInfo khInfo=new KhInfo();
        khInfo.setName(name);
        ArrayList<KhInfo> khInfoByName = khInfoService.getKhInfoByName(khInfo);
        if (khInfoByName.size()>0){
            for (KhInfo khInfo1 : khInfoByName){
                Object[] rowData={khInfo1.getId(),khInfo1.getName(),khInfo1.getTel()};
                dtm.addRow(rowData);
            }
        }else {
            JOptionPane.showMessageDialog(null,"没有找到该客户");
       }

    }

    private void table1MouseClicked(MouseEvent e) {
        // TODO add your code here  获取选中行
        int row=table1.getSelectedRow();
        //得到列的数据
        DefaultTableModel dtm=(DefaultTableModel)table1.getModel();
        int chooseId = (int) dtm.getValueAt(row, 0);
        String name=(String)dtm.getValueAt(row,1);
        String tel=(String)dtm.getValueAt(row,2);
        textField2.setText(name);
        textField3.setText(tel);
    }
    private void button2(ActionEvent e) {
        // TODO add your code here 修改客户信息
        String name=textField2.getText();
        String tel=textField3.getText();
        DefaultTableModel dtm=(DefaultTableModel)table1.getModel();
        int row=table1.getSelectedRow();
        int id = (int) dtm.getValueAt(row, 0);
        //修改客户信息
        if (StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null,"客户名称不能为空");
        }
        if (StringUtil.isEmpty(tel)){
            JOptionPane.showMessageDialog(null,"客户电话不能为空");
        }
        KhInfo khInfo=new KhInfo(id,name,tel);
        Boolean updateKhInfo = khInfoService.updateKhInfo(khInfo);
        if (updateKhInfo){
            JOptionPane.showMessageDialog(null,"修改成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null,"修改失败");
        }
    }

    private void button3(ActionEvent e) {
        // TODO add your code here 删除事件
        int row=table1.getSelectedRow();
        DefaultTableModel dtm=(DefaultTableModel)table1.getModel();
        int id = (int) dtm.getValueAt(row, 0);
        Boolean deleteKhInfo = khInfoService.deleteKhInfo(id);
        if (deleteKhInfo){
            JOptionPane.showMessageDialog(null,"删除成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null,"删除失败");
        }
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        label3 = new JLabel();
        textField2 = new JTextField();
        textField3 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();

        //======== this ========
        setTitle("\u7ba1\u7406\u5ba2\u6237\u8868\u5355");
//        setResizable(false);
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5ba2\u6237\u540d\u5b57");

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                    {null, null, null},
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd"
                }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u5ba2\u6237\u540d\u5b57");

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> {
            button2(e);

        });

        //---- label3 ----
        label3.setText("\u5ba2\u6237\u7535\u8bdd");

        //---- button3 ----
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> {
			button3(e);

		});

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(55, 55, 55)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label2)
                            .addGap(56, 56, 56)
                            .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
                            .addGap(81, 81, 81)
                            .addComponent(button2))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(label3)
                            .addGap(56, 56, 56)
                            .addComponent(textField3, GroupLayout.PREFERRED_SIZE, 140, GroupLayout.PREFERRED_SIZE)
                            .addGap(81, 81, 81)
                            .addComponent(button3))
                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                            .addGroup(contentPaneLayout.createSequentialGroup()
                                .addComponent(label1)
                                .addGap(36, 36, 36)
                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 235, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(button1, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE))
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 476, GroupLayout.PREFERRED_SIZE)))
                    .addGap(49, 49, 49))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(1, 1, 1)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label1)
                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(button1)))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 187, GroupLayout.PREFERRED_SIZE)
                    .addGap(7, 7, 7)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(7, 7, 7)
                            .addComponent(label2))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(1, 1, 1)
                            .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addComponent(button2))
                    .addGap(21, 21, 21)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(7, 7, 7)
                            .addComponent(label3))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(1, 1, 1)
                            .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addComponent(button3)))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JTextField textField2;
    private JButton button2;
    private JLabel label3;
    private JTextField textField3;
    private JButton button3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
